<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>夏栀来信 - 订单结算</title>
    <link rel="stylesheet" href="css/style.css">
    <style>
        .checkout-container {
            max-width: 1000px;
            margin: 30px auto;
            padding: 20px;
            display: flex;
            gap: 30px;
        }
        .order-summary {
            flex: 2;
            background: #f9f9f9;
            padding: 25px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        .user-actions {
            flex: 1;
        }
        .order-title {
            font-size: 24px;
            margin-bottom: 20px;
            padding-bottom: 15px;
            border-bottom: 1px solid #eee;
        }
        .order-item {
            display: flex;
            justify-content: space-between;
            padding: 15px 0;
            border-bottom: 1px solid #eee;
        }
        .order-item:last-child {
            border-bottom: none;
        }
        .order-total {
            font-weight: bold;
            font-size: 18px;
            margin-top: 20px;
        }
        .payment-methods {
            margin-top: 30px;
        }
        .payment-methods h3 {
            margin-bottom: 15px;
        }
        .payment-option {
            display: flex;
            align-items: center;
            margin-bottom: 10px;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 4px;
            cursor: pointer;
        }
        .payment-option.selected {
            border-color: #27ae60;
            background-color: #f0fff4;
        }
        .payment-option input {
            margin-right: 10px;
        }
        .confirm-btn {
            width: 100%;
            background-color: #27ae60;
            color: white;
            border: none;
            padding: 12px;
            border-radius: 4px;
            font-size: 16px;
            cursor: pointer;
            margin-top: 20px;
            transition: background-color 0.3s;
        }
        .confirm-btn:hover {
            background-color: #2ecc71;
        }
        .user-panel {
            background: #f9f9f9;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        .user-panel h3 {
            margin-bottom: 15px;
            padding-bottom: 10px;
            border-bottom: 1px solid #eee;
        }
        .user-menu {
            list-style: none;
            padding: 0;
        }
        .user-menu li {
            margin-bottom: 10px;
        }
        .user-menu a {
            display: block;
            padding: 10px;
            color: #333;
            text-decoration: none;
            border-radius: 4px;
            transition: background-color 0.3s;
        }
        .user-menu a:hover {
            background-color: #f0f0f0;
        }
        .user-menu a.active {
            background-color: #e0f7fa;
            color: #007bff;
        }
        .modal {
            display: none;
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background-color: rgba(0,0,0,0.5);
            z-index: 1000;
            justify-content: center;
            align-items: center;
        }
        .modal-content {
            background-color: white;
            padding: 30px;
            border-radius: 8px;
            text-align: center;
            max-width: 500px;
        }
        .modal-content img {
            max-width: 100%;
            margin-bottom: 20px;
        }
        .modal-close {
            background-color: #27ae60;
            color: white;
            border: none;
            padding: 10px 20px;
            border-radius: 4px;
            cursor: pointer;
            margin-top: 20px;
        }
    </style>
</head>
<body>
    <header>
        <div class="top-bar">
            <span>学号: 23215220329 姓名: 牟其鑫</span>
            <div class="user-actions">
                <a href="login.html">登录</a> | <a href="register.html">注册</a>
            </div>
        </div>
        <div class="main-header">
            <div class="logo">夏栀来信</div>
            <div class="search-box">
                <form id="search-form" action="product.html" method="get">
                    <input type="text" name="name" placeholder="搜索商品名称..." required>
                    <button type="submit">搜索</button>
                </form>
            </div>
            <div class="cart-icon">
                <a href="cart.html">购物车(<span id="cart-count">0</span>)</a>
            </div>
        </div>
        <nav>
            <ul>
                <li><a href="index.html">首页</a></li>
                <li><a href="#">热门推荐</a></li>
                <li><a href="#">节日限定</a></li>
                <li><a href="#">园艺工具</a></li>
                <li><a href="#">养护指南</a></li>
                <li><a href="#">常见问题</a></li>
            </ul>
        </nav>
    </header>

    <main class="checkout-container">
        <section class="order-summary">
            <h2 class="order-title">订单确认</h2>
            
            <div id="order-items">
                <!-- 订单商品将通过JS动态加载 -->
            </div>
            
            <div class="order-total">
                <div class="order-item">
                    <span>应付总额：</span>
                    <span id="order-total-price">¥0.00</span>
                </div>
            </div>
            
            <div class="payment-methods">
                <h3>选择支付方式</h3>
                <label class="payment-option selected">
                    <input type="radio" name="payment" value="wechat" checked> 
                    微信支付
                </label>
                <label class="payment-option">
                    <input type="radio" name="payment" value="alipay"> 
                    支付宝
                </label>
                <label class="payment-option">
                    <input type="radio" name="payment" value="bank"> 
                    银行卡支付
                </label>
            </div>
            
            <button id="confirm-payment" class="confirm-btn">确认支付</button>
        </section>
        
        <section class="user-actions">
            <div class="user-panel">
                <h3>个人中心</h3>
                <ul class="user-menu">
                    <li><a href="profile.html">个人资料</a></li>
                    <li><a href="orders.html" class="active">我的订单</a></li>
                    <li><a href="address.html">收货地址</a></li>
                    <li><a href="login.html">退出登录</a></li>
                </ul>
            </div>
        </section>
    </main>

    <!-- 支付成功弹窗 -->
    <div id="payment-modal" class="modal">
        <div class="modal-content">
            <img src="images/m.jpg" alt="支付成功">
            <h2>请先支付！</h2>
            <p>您的订单已成功打印，待支付成功后我们将尽快为您发货</p>
            <p>订单号: <span id="order-number">20230615001</span></p>
            <button class="modal-close" id="close-modal">返回首页</button>
        </div>
    </div>

    <footer>
        <div class="footer-content">
            <p>© 2023 夏栀来信网上购物商城 版权所有</p>
            <p>地址：某某省某某市某某区某某路123号</p>
            <p>联系电话：123-45678901</p>
        </div>
    </footer>

    <script>
    document.addEventListener('DOMContentLoaded', function() {
        // 从本地存储加载购物车
        let cart = JSON.parse(localStorage.getItem('cart')) || [];
        let orderItems = document.getElementById('order-items');
        let orderTotalPrice = document.getElementById('order-total-price');
        
        // 更新购物车数量显示（供其他页面调用）
        window.updateCartCount = function() {
            const count = cart.reduce((total, item) => total + item.quantity, 0);
            document.getElementById('cart-count').textContent = count;
        };
        
        // 渲染订单商品
        function renderOrderItems() {
            orderItems.innerHTML = '';
            
            if (cart.length === 0) {
                orderItems.innerHTML = `
                    <tr>
                        <td colspan="5">
                            <div class="empty-cart">
                                <p>您的购物车是空的</p>
                                <a href="index.html" class="continue-shopping">继续购物</a>
                            </div>
                        </td>
                    </tr>
                `;
                orderTotalPrice.textContent = '¥0.00';
                updateCartCount();
                return;
            }
            
            let subtotal = 0;
            
            cart.forEach(item => {
                const itemTotal = item.price * item.quantity;
                subtotal += itemTotal;
                
                const row = document.createElement('tr');
                row.innerHTML = `
                    <td>
                        <div class="cart-item-info">
                            <img src="${item.image}" alt="${item.name}">
                            <span>${item.name}</span>
                        </div>
                    </td>
                    <td>¥${item.price.toFixed(2)}</td>
                    <td>
                        <input type="number" min="1" value="${item.quantity}" 
                               data-id="${item.id}" class="quantity-input">
                    </td>
                    <td>¥${itemTotal.toFixed(2)}</td>
                    <td><button class="remove-item" data-id="${item.id}">删除</button></td>
                `;
                orderItems.appendChild(row);
            });
            
            // 计算总额（免运费）
            const total = subtotal;
            orderTotalPrice.textContent = `¥${total.toFixed(2)}`;
        }
        
        // 初始渲染
        renderOrderItems();
        
        // 支付方式选择
        document.querySelectorAll('.payment-option input').forEach(radio => {
            radio.addEventListener('change', function() {
                document.querySelectorAll('.payment-option').forEach(option => {
                    option.classList.remove('selected');
                });
                this.parentElement.classList.add('selected');
            });
        });
        
        // 确认支付按钮
        document.getElementById('confirm-payment').addEventListener('click', function() {
            if (cart.length === 0) {
                alert('您的订单中没有商品');
                return;
            }
            
            // 生成订单数据（使用新的变量名避免重复声明）
            const orderDate = new Date();
            const newOrderNumber = orderDate.getFullYear() + 
                                 String(orderDate.getMonth() + 1).padStart(2, '0') + 
                                 String(orderDate.getDate()).padStart(2, '0') + 
                                 String(orderDate.getHours()).padStart(2, '0') + 
                                 String(orderDate.getMinutes()).padStart(2, '0') + 
                                 String(orderDate.getSeconds()).padStart(2, '0');
            
            const order = {
                id: newOrderNumber,
                date: orderDate.toLocaleString(),
                items: [...cart],
                total: cart.reduce((sum, item) => sum + (item.price * item.quantity), 0),
                status: '已支付',
                address: JSON.parse(localStorage.getItem('defaultAddress')) || {}
            };

            // 保存订单到本地存储
            let orders = JSON.parse(localStorage.getItem('orders')) || [];
            orders.unshift(order); // 新订单添加到前面
            localStorage.setItem('orders', JSON.stringify(orders));
            
            // 显示支付成功弹窗
            document.getElementById('order-number').textContent = newOrderNumber;
            document.getElementById('payment-modal').style.display = 'flex';
            
            // 清空购物车
            localStorage.setItem('cart', JSON.stringify([]));
            updateCartCount();
        });

        // 关闭弹窗按钮
        document.getElementById('close-modal').addEventListener('click', function() {
            window.location.href = 'index.html';
        });
        
        // 更新购物车数量
        updateCartCount();
    });
</script>
</body>
</html>